﻿var id= null;
var chartname = null;
var charturl = null;
var chartpath = null;
var chartorder = null;
var productdetails = null;
//记录页面bootstrap-table全局变量$table，方便应用
$("#rotationchartTable").bootstrapTable({
    url: reqth+"/rotationchart/allRotationchart",
    method: 'post',                      //请求方式（*）
    striped: true,                      //是否显示行间隔色
    toolbar: '#toolbar',                //工具按钮用哪个容器
    search: true ,                     //是否显示表格搜索
    showRefresh: true,                  //是否显示刷新按钮
    showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
    pagination: true,                   //是否显示分页（*）
    sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
    pageList: [1,2],                    //可供选择的每页的行数（*）
    showColumns: true,                  //是否显示所有的列（选择显示的列）
    minimumCountColumns: 2,             //最少允许的列数

    columns: [{
        title: '序号',
        formatter: function one (value, row, index) {
            return index+1;
        }
    },{
        field: 'chartname',
        title: '图片名称',
        sortable: true
    },{
        field: 'charturl',
        title: '图片超链接'
    },{
        field: 'chartpath',
        title: '图片',
        formatter : function (value, row, index) {
            return "<img style='width: 50px;height: 50px;' src='http://localhost:8080/reception/assets/imgs/slider/新鲜水果11.jpg' alt=''>"
        }
    },{
        field: 'chartorder',
        title: '商品排序'
    },{
        field: 'productdetails',
        title: '图片详情'
    },{
        field:'id',
        title: '操作',
        width: 120,
        align: 'center',
        valign: 'middle',
        formatter:function(value, row, index) {
            var id = value;
            var result = "";
            result += "<a href='javascript:;' class='btn btn-xs green' onclick=\"aEditViewById('" + id + "', view='view')\" title='查看'><span class='glyphicon glyphicon-search'></span></a>";
            result += "<a href='javascript:;' class='btn btn-xs blue' title='编辑' data-toggle='modal' data-target='.bs-example-modal-lg'><span class='glyphicon glyphicon-pencil'></span></a>";
            result += "<a href='javascript:;' class='btn btn-xs red' onclick=\"DeleteByIds('" + id + "')\" title='删除' ><span class='glyphicon glyphicon-remove'></span></a>";
            return result;
        },
        events:{
            'click a[title=编辑]': function (e, value, row, index) {
                $("#myModalLabel").text("编辑");
                $("#id").val(row.id);
                $("#chartname").val(row.chartname);
                $("#charturl").val(row.charturl);
                $("#chartpath").val(row.chartpath);
                $("#chartorder").val(row.chartorder);
                $("#productdetails").val(row.productdetails);
                $("#mod-but").attr("onclick","updateById()");
            },
            'click a[title=删除]': function (e, value, row, index) {
                var loadingIndex = null;
                layer.confirm("是否删除", {
                    icon: 3,
                    title: '提示'
                }, function(cindex) {
                    dataleFile(row.chartpath);
                    $.ajax({
                        type: "POST",
                        url: reqth + "rotationchart/deltePermission",
                        data: {
                            id:row.id
                        },
                        beforeSend: function() {
                            loadingIndex = layer.msg('处理中', {
                                icon: 16
                            });
                        },
                        success: function(result) {
                            layer.close(loadingIndex);
                            if (result.success) {
                                layer.msg("删除成功！", {
                                    time: 2000,
                                    icon: 6,
                                    shift: 6
                                }, function() {
                                    $("#rotationchartTable").bootstrapTable('refresh');
                                });
                            } else {
                                layer.msg("删除失败！", {
                                    time: 2000,
                                    icon: 5,
                                    shift: 6
                                }, function() {});
                            }
                        }
                    });
                });
            },
        }
    },]
});
function add(){
    $("#myModalLabel").text("添加");
    $("#id").val("");
    $("#chartname").val("");
    $("#charturl").val("");
    $("#chartpath").val("");
    $("#chartorder").val("");
    $("#productdetails").val("");
    $("#mod-but").attr("onclick","insertById()");
}
function updateById(){
    id = $("#id").val();
    chartname = $("#chartname").val();
    charturl = $("#charturl").val();
    chartpath = $("#chartpath").val();
    chartorder = $("#chartorder").val();
    productdetails = $("#productdetails").val();
    $.ajax({
        type:'post',
        url:reqth+"rotationchart/update",
        data:{
            id:id,
            chartname:chartname,
            charturl:charturl,
            chartpath:chartpath,
            chartorder:chartorder,
            productdetails:productdetails
        },success:function (result){
            if (result.success){
                $("#rotationchartTable").bootstrapTable('refresh');
                layer.msg("修改成功！", {
                    time: 2000,
                    icon: 6,
                    shift: 6
                });
            }else{
                layer.msg("修改失败！", {
                    time: 2000,
                    icon: 5,
                    shift: 6
                });
            }
        }
    })
}
//删除上传文件
function dataleFile(url){
    let path ="C:/Users/Wzzzzzhan/Desktop/reception/"
    $.ajax({
        type: "POST",
        url: reqth + "Download/delFile",
        data: {
            path:path+url
        },
        success: function(result) {
            return result;
        }
    });
}
function uploadFile(name,path){
    var fileObj = document.getElementById("file");
    // H5 FormData 对象
    var form = new FormData();
    form.append("file", fileObj.files[0]); // 文件对象
    if (fileObj.files[0] == null) {
        // layer.alert("文件为空!")
        return false;
    }
    form.append("name",name);
    form.append("path", shopReq+path);
    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.open("post", reqth + "Download/upload");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if (xhr.responseText == "true") {
                fileboolean = true;
            }
        }
    };
    xhr.send(form);
}
function insertById(){
    id = $("#id").val();
    chartname = $("#chartname").val();
    charturl = $("#charturl").val();
    chartpath = $("#chartpath").val();
    chartorder = $("#chartorder").val();
    productdetails = $("#productdetails").val();
    uploadFile(chartname,"slider");
    $.ajax({
        type: "POST",
        url: reqth + "rotationchart/add",
        data: {
            chartname:chartname,
            charturl:charturl,
            chartpath:chartpath,
            chartorder:chartorder,
            productdetails:productdetails
        },
        beforeSend: function() {
            loadingIndex = layer.msg('处理中', {
                icon: 16
            });
        },
        success: function(result) {
            layer.close(loadingIndex);
            $("#rotationchartTable").bootstrapTable('refresh');
            if (result.success) {
                layer.msg("添加成功！", {
                    time: 2000,
                    icon: 6,
                    shift: 6
                }, function() {
                });
            } else {
                layer.msg("添加失败！", {
                    time: 2000,
                    icon: 5,
                    shift: 6
                }, function() {});
            }
        }
    });
}
